﻿@model JewererBasic.Models.NewMCViewModel
<h2>Material Control</h2>

<fieldset>
    @using (Html.BeginForm("BagControl", "MC", FormMethod.Post, new { id = "formBagControl" }))
    {    
        <input type="text" id="txtBagId" name="txtBagId" style="width: 200px;" class="Number" />
        <input type="submit" id="buttonSearch" name="submit" value="Search" />
        if (Model != null)
        {               
        @Html.HiddenFor(m => m.Bag.Id, new { id = "hiddenBagId" })
            if (Model.Bag.Finish == -1)
            {
        <input type="submit" id="buttonStart" name="submit" value="Start" />
        <p><strong class="myStrongAnnounce">This bag has not started. To START it, click "Start" button</strong></p>
            }
            else if (Model.Bag.Finish == 1)
            {            
        <input type="submit" id="buttonRestart" name="submit" value="Restart" />
        <p><strong class="myStrongAnnounce">This bag has finished</strong></p>
        <p><strong class="myStrongAnnounce">Click "Restart" if you want rework this bag</strong></p>
            }
            else if (Model.Bag.Finish == 0)
            {            
        <input type="submit" id="buttonFinish" name="submit" value="Finish" />
        <p><strong class="myStrongAnnounce">This bag is starting. To FINISH it, click "Finish" button</strong></p>
        <fieldset style="width: 800px">
            <legend>Process Information</legend>
            <table>
                <tr align="top">
                    <td style="width: 500px">
                        @if (Model.Current_Bag_Process != null)
                        {
                            @Html.HiddenFor(m => m.Current_Bag_Process.IdProcess, new { id = "currentProcessId" })
                            @Html.HiddenFor(m => m.Current_Bag_Process.IdBag)
                            @Html.HiddenFor(m => m.Current_Bag_Process_History.Id)
                            <table>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Current Weight: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Bag.Current_Weight)
                                        @Html.HiddenFor(m => m.Bag.Current_Weight)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Current Process: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process.PROCESS.Name, new { id = "currentProcess" })
                                        @Html.HiddenFor(m => m.Current_Bag_Process.PROCESS.Name)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Current Weight IN: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process.WeightIn)
                                        @Html.HiddenFor(m => m.Current_Bag_Process.WeightIn, new { id = "currentWeightIn" })
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Current Worker ID</strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process.EMPLOYEE.Staff_Id)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Start Day: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process.DateIn)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Current MC: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process.MC)
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Weight Out: </strong>
                                    </td>
                                    <td>
                                        @Html.TextBoxFor(m => m.Current_Bag_Process.WeightOut, new { id = "currentWeightOut", @class = "Number" })
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Reject: </strong>
                                    </td>
                                    <td>
                                        @Html.TextBoxFor(m => m.Current_Bag_Process.RejectWeight, new { id = "currentReject", @class = "Number" })
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <strong class="myStrong">Reason Rework: </strong>
                                    </td>
                                    <td>
                                        @Html.DisplayFor(m => m.Current_Bag_Process_History.ReasonRework)
                                        @Html.HiddenFor(m => m.Current_Bag_Process_History.ReasonRework)
                                    </td>
                                </tr>
                            </table>
                        }
                        else
                        {
                            <strong class="myStrongAnnounce">There are no current process</strong>
                        }
                    </td>
                    <td style="width: 500px">
                        <table>
                            <tr>
                                <td>
                                    <strong class="myStrong">Next Process: </strong>
                                </td>
                                <td>
                                    @Html.DropDownListFor(m => m.Next_Bag_Process.IdProcess, ViewBag.ListBagProcess as SelectList, new { id = "nextProcess" })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong class="myStrong">Next Weight In</strong>
                                </td>
                                <td>
                                    @Html.TextBoxFor(m => m.Next_Bag_Process.WeightIn, new { id = "txtNextWeightIn", @class = "Number" })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong class="myStrong">Next MC</strong>
                                </td>
                                <td>
                                    @Html.DisplayFor(m => m.Next_Bag_Process.MC)
                                    @Html.HiddenFor(m => m.Next_Bag_Process.MC)
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong class="myStrong">Next Worker ID</strong>
                                </td>
                                <td>
                                    @Html.DropDownListFor(m => m.Next_Bag_Process.WorkerId, ViewBag.Workers as SelectList, new { id = "txtNextWorkerId" })
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong class="myStrong">Start Day: </strong>
                                </td>
                                <td>
                                    @Html.DisplayFor(m => m.Next_Bag_Process.DateIn)
                                    @Html.HiddenFor(m => m.Next_Bag_Process.DateIn)
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong class="myStrong">Reason Rework: </strong>
                                </td>
                                <td>
                                    @Html.TextAreaFor(m => m.Next_Bag_Process_History.ReasonRework, new { id = "trReworkReason", rows = 5, cols = 35 })
                                    @Html.HiddenFor(m => m.Next_Bag_Process_History.ReasonRework)
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </fieldset>
        <input type="submit" id="buttonDone" value="Done" name="submit" />
            }
        <fieldset>
            <legend>Bag Information</legend>
            <table class="table">
                <tr>
                    <td style="width: 50px">
                        <strong class="myStrong">Bag Code</strong>
                    </td>
                    <td style="width: 50px">
                        <a href="@Url.Action("BagProcessDetail", "Order", new { id = Model.Bag.Id })" class="myA">@Model.Bag.Id</a>
                    </td>
                    <td style="width: 50px">
                        <strong class="myStrong">Order Id</strong>
                    </td>
                    <td style="width: 50px">
                        <a href="@Url.Action("Details", "Order", new { id = Model.Bag.ORDER_PRODUCT.IdOrder })" class="myA">@Model.Bag.ORDER_PRODUCT.IdOrder</a>
                    </td>
                    <td style="width: 50px">
                        <strong class="myStrong">Production Card</strong>
                    </td>
                    <td style="width: 50px">
                        @Html.DisplayFor(m => m.Bag.ORDER_PRODUCT.IdProduct)
                    </td>
                    <td style="width: 50px">
                        <strong class="myStrong">Qty</strong>
                    </td>
                    <td style="width: 50px">
                        @Html.DisplayFor(m => m.Bag.Number)
                    </td>
                    <td style="width: 50px">
                        <strong class="myStrong">Finish Week</strong>
                    </td>
                    <td style="width: 50px">
                        @Html.DisplayFor(m => m.Bag.ORDER_PRODUCT.ORDER.FinishWeek)
                    </td>
                </tr>
            </table>
        </fieldset>
            if (Model.Bag.BAG_PROCESS != null)
            {
        <fieldset>
            <legend>Bag Process</legend>
            <table class="table head">
                <tr>
                    <th style="width: 200px">Process Name
                    </th>
                    <th style="width: 70px">Time
                    </th>
                    <th style="width: 150px">Worker Id
                    </th>
                    <th style="width: 150px">Weight IN
                    </th>
                    <th style="width: 200px">Date In
                    </th>
                    <th style="width: 150px">WeightOut
                    </th>
                    <th style="width: 200px">Date Out
                    </th>
                    <th style="width: 150px">MC
                    </th>
                </tr>
                @foreach (var item in Model.Bag.BAG_PROCESS)
                {
                    if (item.PROCESS.Type == 1)
                    {
                    <tr>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.PROCESS.Name)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.Time)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.EMPLOYEE.Staff_Id)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.WeightIn)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.DateIn)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.WeightOut)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.DateOut)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.MC)
                        </td>
                    </tr>
                    }
                }
            </table>
        </fieldset>
            }
            if (Model.Bag.BAG_PROCESS_HISTORY != null)
            {
        <fieldset>
            <legend>Bag History</legend>
            <table class="table head">
                <tr>
                    <th style="width: 200px">Process Name
                    </th>
                    <th style="width: 150px">Worker Id
                    </th>
                    <th style="width: 150px">Weight IN
                    </th>
                    <th style="width: 200px">Date In
                    </th>
                    <th style="width: 150px">WeightOut
                    </th>
                    <th style="width: 200px">Date Out
                    </th>
                    <th style="width: 150px">MC
                    </th>
                    <th style="width: 150px">Reason Rework
                    </th>
                </tr>
                @foreach (var item in Model.Bag.BAG_PROCESS_HISTORY)
                {
                    <tr>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.PROCESS.Name)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.EMPLOYEE.Staff_Id)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.WeigthIn)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.DateIn)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.WeightOut)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.DateOut)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.MC)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.ReasonRework)
                        </td>
                    </tr>
                }
            </table>
        </fieldset>
            }
        }
        <input type="hidden" id="nameSubmitButton" />
    }
</fieldset>
@section Scripts {
    <script type="text/javascript">
        $(this).load(function () {
            var bagId = $('#hiddenBagId').val();
            $('#txtBagId').val(bagId);

            //var newAddress = '@Url.Action("ExportReportBagHistory", "Report")' + '?bagId=' + bagId;
            //$('#aPrintBagHistory').attr('href', newAddress);

            // Hide rework
            $('#trReworkReason').prop("disabled", true);

            // In current process, If there are not weight in, there are not weight out too.
            if ($('#currentWeightIn').val() == "") {
                $('#currentWeightOut').prop("disabled", true);
                $('#currentReject').prop("disabled", true);
            }

            // Must check if Wax if rework on startup
        });

        // On change in Process combobox
        $('#nextProcess').on('change', function (e) {
            var optionSelected = $("option:selected", this);
            var nextProcess = this.value;
            var currentProcess = $('#currentProcessId').val();
            if (nextProcess == currentProcess) {
                alert("Can't choose this process, You must finish this process before rework it!");
                $('#nextProcess').prop('selectedIndex', 0);
                return;
            }
            // check if waiting process
            $.ajax({
                url: '/MC/CheckIfWaitingProcess',
                data: { processId: nextProcess },
                dataType: 'json',
                type: 'GET',
                async: false,
                success: function (data) {
                    if (data.isWaiting) {
                        $('#trReworkReason').prop("disabled", true);
                        $('#txtNextWeightIn').prop("disabled", true);
                        $('#txtNextWorkerId').prop("disabled", true);
                    } else {
                        $('#txtNextWeightIn').prop("disabled", false);
                        $('#txtNextWorkerId').prop("disabled", false);
                        // check if rework
                        var bagId = $('#hiddenBagId').val();
                        $.ajax({
                            url: '/MC/CheckIfRework',
                            data: { processId: nextProcess, bagId: bagId },
                            dataType: 'json',
                            type: 'GET',
                            async: false,
                            success: function (data) {
                                if (data.isRework) {
                                    $('#trReworkReason').prop("disabled", false);
                                }
                                else {
                                    $('#trReworkReason').prop("disabled", true);
                                }
                            }
                        });
                    }
                }
            });
        });
    </script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("form").on('keydown', 'input[class$=\'Number\']', function (event) {
                if (jQuery.inArray(event.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
                (event.keyCode == 65 && event.ctrlKey === true) ||
                (event.keyCode >= 35 && event.keyCode <= 40)
                    ) {
                    return;
                }
                else {
                    if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                        event.preventDefault();
                    }
                    else {
                        var $focused = $(':focus');
                        if ($focused.val().length >= 10) {
                            $focused.keydown(function (event) {
                                event.preventDefault();
                            })
                        }
                    }
                }
            });
        });
    </script>

    <script type="text/javascript">
        $('#buttonSearch').click(function () {
            $('#nameSubmitButton').val("Search");
        });
        $('#buttonDone').click(function () {
            $('#nameSubmitButton').val("Done");
        });
        $('#buttonStart').click(function () {
            $('#nameSubmitButton').val("Start");
        });
        $('#buttonFinish').click(function () {
            $('#nameSubmitButton').val("Finish");
        });
        $('#buttonRestart').click(function () {
            $('#nameSubmitButton').val("Restart");
        });
        $(document).ready(function () {
            $("form").submit(function (evt) {
                var isValid = true;
                var namebutton = $('#nameSubmitButton').val();
                if (namebutton == "Search") {
                    var bagId = $('#txtBagId').val();
                    if (!bagId) {
                        alert("Please enter Bag Code!");
                        isValid = false;
                    } else {
                        $.ajax({
                            url: '/MC/FindBag',
                            data: { bagId: bagId },
                            dataType: 'json',
                            type: 'GET',
                            async: false,
                            success: function (data) {
                                if (data.Available == false) {
                                    alert("Can't find this bag!");
                                    $('#txtBagId').focus();
                                    isValid = false;
                                }
                            }
                        });
                    }
                } else if (namebutton == "Done") {
                    // There are weight in without weight out in current process
                    var currentWeightIn = $('#currentWeightIn').val();
                    var currentWeightOut = $('#currentWeightOut').val();
                    if (currentWeightIn) {
                        if (!currentWeightOut) {
                            alert("Please enter Current Weight Out!");
                            $('#currentWeightOut').focus();
                            isValid = false;
                        }
                    }

                    // next process and current process are not the same
                    var nextProcess = $('#nextProcess').val();
                    var currentProcess = $('#currentProcessId').val();
                    if (nextProcess == currentProcess) {
                        alert("Next Process and Current Process must not the same!");
                        isValid = false;
                    }

                    // if next process !=waiting then worker id is required
                    if ($('#txtNextWorkerId').attr('disabled') != 'disabled' && !$('#txtNextWorkerId').val()) {
                        alert("Please enter Worker Id!");
                        $('#txtNextWorkerId').focus();
                        isValid = false;
                    }
                    // if rework then reason rework is required
                    if ($('#trReworkReason').attr('disabled') != 'disabled' && !$('#trReworkReason').val()) {
                        alert("Please enter Rework Reason!");
                        $('#trReworkReason').focus();
                        isValid = false;
                    }
                } else if (namebutton == "Start") {

                } else if (namebutton == "Finish") {
                    // There are weight in without weight out in current process
                    var currentWeightIn = $('#currentWeightIn').val();
                    var currentWeightOut = $('#currentWeightOut').val();
                    if (currentWeightIn) {
                        if (!currentWeightOut) {
                            alert("Please enter Current Weight Out!");
                            $('#currentWeightOut').focus();
                            isValid = false;
                        } else if (parseFloat(currentWeightIn) > parseFloat(currentWeightOut)) {
                            alert("Weight Out is less than Weight In???");
                            // isValid=false;
                            //incomplete
                        }
                    }
                } else if (namebutton == "Restart") {

                }
                else {
                    alert("Undefined button submit");
                    isValid = false;
                }
                if (isValid == false) {
                    evt.preventDefault();
                }
            });
        });
    </script>
}

